package ks.client.lobby;


import java.awt.GridBagLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.GridBagConstraints;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;

import ks.client.UserContext;
import ks.client.controllers.ConnectController;
import ks.client.controllers.DisconnectController;
import ks.client.controllers.ProcessLobbyInputController;

public class RoomGUI extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel topPanel = null;
	private JLabel userL = null;
	private JTextField userField = null;
	private JLabel passwordLabel = null;
	private JPasswordField passwordField = null;
	private JTextField lobbyInput = null;
	private java.awt.Component userManagerGUI = null;
	private java.awt.Component  tableManagerGUI = null;
	private JLabel jLabel1 = null;
	private JScrollPane tblScrollPanel = null;
	private JScrollPane userScrollPanel = null;
	private JScrollPane lobbyScrollbarPanel = null;
	private JTextArea lobbyOutput = null;
	private JCheckBox selfRegisterCheck = null;
	private JButton connectButton = null;

	/** Lobby in which we are enclosed. */
	Lobby lobby;
	
	/** Context. */
	UserContext context;
	
	/**
	 * This is the default constructor
	 */
	public RoomGUI(Lobby lobby) {
		super();
		
		this.lobby = lobby;
		initialize();
	}


	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(850, 710);
		this.setLayout(null);
		this.add(getTopPanel(), null);
		this.add(getTblScrollPanel(), null);
		this.add(getUserScrollPanel(), null);
		this.add(getLobbyScrollbarPanel(), null);
		this.add(getLobbyInput(), null);
	}

	/**
	 * This method initializes topPanel	
	 * 	
	 * @return java.awt.Panel	
	 */
	JPanel getTopPanel() {
		if (topPanel == null) {
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 7;
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.gridy = 0;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints6.gridy = 0;
			gridBagConstraints6.weightx = 0.05;
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.gridx = 6;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 5;
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.gridy = 0;
			passwordLabel = new JLabel();
			passwordLabel.setText("Password:");
			passwordLabel.setName("passwordLabel");
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.weightx = 0.0;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.gridx = 4;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 3;
			gridBagConstraints3.gridy = 0;
			userL = new JLabel();
			userL.setText("User:");
			userL.setName("userLabel");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.gridwidth = 2;
			gridBagConstraints1.gridy = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.anchor = GridBagConstraints.EAST;
			gridBagConstraints8.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints8.gridy = 0;
			gridBagConstraints8.gridx = 6;
			
			topPanel = new JPanel();
			topPanel.setLayout(new GridBagLayout());
			topPanel.setBackground(SystemColor.control);
			topPanel.setBounds(10,10, 820, 37);
			
			topPanel.add(userL, gridBagConstraints3);
			topPanel.add(getUserField(), gridBagConstraints4);
			topPanel.add(passwordLabel, gridBagConstraints5);
			topPanel.add(getPasswordField(), gridBagConstraints6);
			topPanel.add(getConnectButton(), gridBagConstraints7);

			topPanel.add(getSelfRegisterCheck(), gridBagConstraints8);
		}
		return topPanel;
	}

	
	/**
	 * This method initializes userField	
	 * 	
	 * @return java.awt.TextField	
	 */
	JTextField getUserField() {
		if (userField == null) {
			userField = new JTextField();
			userField.setName("userField");
			userField.setPreferredSize(new Dimension(180, 23));
			userField.setText("UserName");
		}
		return userField;
	}

	/**
	 * This method initializes textField	
	 * 	
	 * @return java.awt.TextField	
	 */
	JPasswordField getPasswordField() {
		if (passwordField == null) {
			passwordField = new JPasswordField();
			passwordField.setPreferredSize(new Dimension(180, 23));
			passwordField.setText("*******");
			passwordField.setName("passwordField");
		}
		return passwordField;
	}

	
	/**
	 * This method initializes userManagerGUI	
	 * 	
	 */
	private java.awt.Component getUserManagerGUI() {
		if (userManagerGUI == null) {
			JLabel jLabel = new JLabel();
			jLabel.setText("To Be Inserted");
			JPanel p = new JPanel();
			p.setLayout(new FlowLayout());
			p.add(jLabel, null);
			userManagerGUI = p;
		}
		return userManagerGUI;
	}

	/**
	 * Will be replaced with EXTERNAL call to retrieve TableManager from the 
	 * TableManagerGUI
	 */
	private java.awt.Component getTableManagerGUI() {
		if (tableManagerGUI == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("To Be Inserted");
			JPanel p = new JPanel();
			p.setLayout(new FlowLayout());
			p.add(jLabel1, null);
			tableManagerGUI = p;
		}
		
		return tableManagerGUI;
	}

	/**
	 * This method initializes tblScrollPanel	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getTblScrollPanel() {
		if (tblScrollPanel == null) {
			tblScrollPanel = new JScrollPane();
			tblScrollPanel.setBounds(new Rectangle(11, 74, 581, 333));
			tblScrollPanel.setViewportView(getTableManagerGUI());
			tblScrollPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			tblScrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		}
		return tblScrollPanel;
	}

	/**
	 * This method initializes userScrollPanel	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getUserScrollPanel() {
		if (userScrollPanel == null) {
			userScrollPanel = new JScrollPane();
			userScrollPanel.setBounds(new Rectangle(603, 73, 239, 635));
			userScrollPanel.setViewportView(getUserManagerGUI());
			userScrollPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			userScrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		}
		return userScrollPanel;
	}

	/**
	 * This method initializes lobbyScrollbarPanel	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getLobbyScrollbarPanel() {
		if (lobbyScrollbarPanel == null) {
			lobbyScrollbarPanel = new JScrollPane();
			lobbyScrollbarPanel.setBounds(new Rectangle(11, 419, 581, 226));
			lobbyScrollbarPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			lobbyScrollbarPanel.setViewportView(getLobbyOutput());
			lobbyScrollbarPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		}
		return lobbyScrollbarPanel;
	}

	/**
	 * This method initializes lobbyOutput	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	public JTextArea getLobbyOutput() {
		if (lobbyOutput == null) {
			lobbyOutput = new JTextArea();
		}
		return lobbyOutput;
	}
	
	/**
	 * This method initializes lobbyOutput	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	JTextField getLobbyInput() {
		if (lobbyInput == null) {
			lobbyInput = new JTextField(80);
			lobbyInput.setBounds(11, 650, 581, 24);
			
			lobbyInput.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					String s = lobbyInput.getText();
					lobbyInput.setText("");

					// hand off to the appropriate controller.
					new ProcessLobbyInputController(context).process(s);
				}
				
			});
		}
		return lobbyInput;
	}

	/**
	 * Append text to the lobby.
	 * 
	 * @param s
	 */
	public void append (String s) {
		String t = lobbyOutput.getText() + "\n" + s;
		lobbyOutput.setText(t);
		scrollToEnd();		
	}
	
	/**
	 * Make sure we can scroll to the end on demand.
	 */
	void scrollToEnd() {
		lobbyOutput.setCaretPosition(lobbyOutput.getDocument().getLength());
	}
	
	/**
	 * Self-register checkbox	
	 * 	
	 * @return javax.swing.JButton	
	 */
	JCheckBox getSelfRegisterCheck() {
		if (selfRegisterCheck == null) {
			selfRegisterCheck = new JCheckBox();
			selfRegisterCheck.setText("Create Account");
		}
		
		return selfRegisterCheck;
	}
	
	/**
	 * This method initializes connectButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	JButton getConnectButton() {
		if (connectButton == null) {
			connectButton = new JButton();
			connectButton.setText("Connect");
			
			connectButton.addActionListener(new ActionListener() {

				/** Attempt to connect via our connection agent. */
				public void actionPerformed(ActionEvent e) {
					if (connectButton.getText().equals("Connect")) {
						String user = getUserField().getText();
						char [] chars = getPasswordField().getPassword();
						
						// eliminates from memory safely
						String password = new String("");
						for (char c : chars) {
							password = password + c;
						}
						for (int i = 0; i < chars.length; i++) {
							chars[i] = '\0';   // protect.
						}
						
						context.setUser(user);
						context.setPassword(password);
						context.setSelfRegister(getSelfRegisterCheck().isSelected());
						
						if (new ConnectController(lobby).process(context)) {
							connectButton.setText("Disconnect");
						}
					} else {
						if (new DisconnectController(lobby).process(context)) {
							connectButton.setText("Connect");
						}
					}
					
				}
			});
		}
		return connectButton;
	}

	/**
	 * Enable the clean reset of login credentials to original user context.
	 * 
	 */
	public void resetLogin() {
		getUserField().setText(context.getUser());
		getPasswordField().setText(context.getPassword());
	}

	

}  //  @jve:decl-index=0:visual-constraint="10,10"
